package com.wyp.didishige.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.wyp.didishige.adapter.NearPeopleAdapter;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.utils.CollectionUtils;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.wiget.xlist.XListView;
import com.wyp.didishige.R;
import com.wyp.didishige.model.User;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.im.task.BRequest;
import cn.bmob.v3.listener.CountListener;
import cn.bmob.v3.listener.FindListener;

/**
 * 附近的人列表
 *
 * @author smile
 * @ClassName: NewFriendActivity
 * @Description: TODO
 * @date 2014-6-6 下午4:28:09
 */
public class NearPeopleActivity extends BaseActivity implements XListView.IXListViewListener, OnItemClickListener {

    XListView mListView;
    NearPeopleAdapter adapter;
    String from = "";
    private String property = null;
    private boolean sex = false;
    List<User> nears = new ArrayList<User>();
    User user;
    private double QUERY_KILOMETERS = 100;//默认查询10公里范围内的人

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_people);
        user = userManager.getCurrentUser(User.class);
        initActionBar();
        initView();
    }

    private void initActionBar() {
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setTitle("附近的人");
    }

    private void initView() {
        initXListView();
    }

    private void initXListView() {
        mListView = (XListView) findViewById(R.id.list_near);
        mListView.setOnItemClickListener(this);
        // 首先不允许加载更多
        mListView.setPullLoadEnable(false);
        // 允许下拉
        mListView.setPullRefreshEnable(true);
        // 设置监听器
        mListView.setXListViewListener(this);
        //
        mListView.pullRefreshing();

        adapter = new NearPeopleAdapter(this, nears);
        mListView.setAdapter(adapter);
        initNearByList(false);
    }


    int curPage = 0;
    ProgressDialog progress;

    private void initNearByList(final boolean isUpdate) {
        if (!isUpdate) {
            progress = new ProgressDialog(NearPeopleActivity.this);
            progress.setMessage("正在查询附近的人...");
            progress.setCanceledOnTouchOutside(true);
            progress.show();
        }

        if (user.getLocation().getLatitude() != 0 && user.getLocation().getLongitude() != 0) {
            double latitude = user.getLocation().getLatitude();
            double longtitude = user.getLocation().getLongitude();
            //封装的查询方法，当进入此页面时 isUpdate为false，当下拉刷新的时候设置为true就行。
            //此方法默认每页查询10条数据,若想查询多于10条，可在查询之前设置BRequest.QUERY_LIMIT_COUNT，如：BRequest.QUERY_LIMIT_COUNT=20
            // 此方法是新增的查询指定10公里内的性别为女性的用户列表，默认包含好友列表
            //如果你不想查询性别为女的用户，可以将equalProperty设为null或者equalObj设为null即可
            userManager.queryKiloMetersListByPage(isUpdate, 0, "location", longtitude, latitude, true, QUERY_KILOMETERS, property, sex, new FindListener<User>() {
                //此方法默认查询所有带地理位置信息的且性别为女的用户列表，如果你不想包含好友列表的话，将查询条件中的isShowFriends设置为false就行
//			userManager.queryNearByListByPage(isUpdate,0,"location", longtitude, latitude, true,"sex",false,new FindListener<User>() {

                @Override
                public void onSuccess(List<User> arg0) {
                    if (CollectionUtils.isNotNull(arg0)) {
                        if (isUpdate) {
                            nears.clear();
                        }
                        adapter.addAll(arg0);
                        if (arg0.size() < BRequest.QUERY_LIMIT_COUNT) {
                            mListView.setPullLoadEnable(false);
                            ShowToast("附近的人搜索完成!");
                        } else {
                            mListView.setPullLoadEnable(true);
                        }
                    } else {
                        ShowToast("暂无附近的人!");
                    }

                    if (!isUpdate) {
                        progress.dismiss();
                    } else {
                        refreshPull();
                    }
                }

                @Override
                public void onError(int arg0, String arg1) {
                    ShowToast("暂无附近的人!");
                    mListView.setPullLoadEnable(false);
                    if (!isUpdate) {
                        progress.dismiss();
                    } else {
                        refreshPull();
                    }
                }

            });
        } else {
            ShowToast("暂无附近的人!");
            progress.dismiss();
            refreshPull();
        }

    }

    /**
     * 查询更多
     */
    private void queryMoreNearList(int page) {
        double latitude = user.getLocation().getLatitude();
        double longtitude = user.getLocation().getLongitude();
        //查询10公里范围内的性别为@sex的用户列表
        userManager.queryKiloMetersListByPage(true, page, "location", longtitude, latitude, true, QUERY_KILOMETERS, property, sex, new FindListener<User>() {
            //查询全部地理位置信息且性别为女性的用户列表
//		userManager.queryNearByListByPage(true,page, "location", longtitude, latitude, true,"sex",false,new FindListener<User>() {

            @Override
            public void onSuccess(List<User> arg0) {
                // TODO Auto-generated method stub
                if (CollectionUtils.isNotNull(arg0)) {
                    adapter.addAll(arg0);
                }
                refreshLoad();
            }

            @Override
            public void onError(int arg0, String arg1) {
                // TODO Auto-generated method stub
                ShowLog("查询更多附近的人出错:" + arg1);
                mListView.setPullLoadEnable(false);
                refreshLoad();
            }

        });
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        User user = (User) adapter.getItem(position);
        Bundle bundle = new Bundle();

        bundle.putString(Config.KEY_USER_ID, user.getObjectId());
        IntentUtil.start_activity(NearPeopleActivity.this, UserInfoActivity.class, bundle);
    }

    @Override
    public void onRefresh() {
        initNearByList(true);
    }

    private void refreshLoad() {
        if (mListView.getPullLoading()) {
            mListView.stopLoadMore();
        }
    }

    private void refreshPull() {
        if (mListView.getPullRefreshing()) {
            mListView.stopRefresh();
        }
    }

    @Override
    public void onLoadMore() {
        double latitude = user.getLocation().getLatitude();
        double longtitude = user.getLocation().getLongitude();
        //这是查询10公里范围内的性别为女用户总数
        userManager.queryKiloMetersTotalCount(User.class, "location", longtitude, latitude, true, QUERY_KILOMETERS, property, sex, new CountListener() {
            //这是查询附近的人且性别为女性的用户总数
//		userManager.queryNearTotalCount(User.class, "location", longtitude, latitude, true,"sex",false,new CountListener() {

            @Override
            public void onSuccess(int arg0) {
                // TODO Auto-generated method stub
                if (arg0 > nears.size()) {
                    curPage++;
                    queryMoreNearList(curPage);
                } else {
                    ShowToast("数据加载完成");
                    mListView.setPullLoadEnable(false);
                    refreshLoad();
                }
            }

            @Override
            public void onFailure(int arg0, String arg1) {
                // TODO Auto-generated method stub
                ShowLog("查询附近的人总数失败" + arg1);
                refreshLoad();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_nearpeople_activity, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }
        if (id == R.id.action_all) {
            property = null;
            onRefresh();
            return true;
        }

        if (id == R.id.action_female) {
            property = "sex";
            sex = false;
            onRefresh();
            return true;
        }
        if (id == R.id.action_male) {
            property = "sex";
            sex = true;
            onRefresh();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
